#ifndef IGSLIB_DIP_SWITCH_H
#define IGSLIB_DIP_SWITCH_H

#include <cstdio>
#include <set>
#include <string>
#include <vector>
#include <bitset>
#include "igslib_idip_switch.h"

namespace igslib 
{
    template <size_t T>
	class CDipSwitch:public IDipSwitch
	{
        protected:
            std::bitset<T> state;
            ISpi* spi;
        public:
            CDipSwitch()
            {
                spi = nullptr;
            }
            virtual bool GetPinState(const int& inx)
            {
                return state.test(inx);
            }
            virtual void SetPinState(const int& inx, const bool& value)
            {
                size_t pos = static_cast<size_t>(inx);
                state.set(pos,value);
            }
            virtual std::vector<bool> GetPinsState()
            {
                std::vector<bool> rtn;
                for ( int i = 0 ; i < state.count() ; ++i )
                {
                    rtn.push_back(GetPinState(i));
                }
                return rtn;
            }
            virtual void SetPinsState(const std::vector<bool>& value)
            {
                for ( size_t i = 0 ; i < value.size() ; ++i )
                {
                    state.set(i,value.at(i));
                }
            }
            virtual void SetPinStateWithValue(const int& value)
            {
                unsigned long init_value = static_cast<unsigned long>(value);
                decltype(state) new_value(init_value);
                state = new_value;
            }
            virtual std::string GetStateString()
            {
                return state.to_string();
            }
            virtual int GetPinStateValue()
            {
                int rtn = 0;
                rtn = static_cast<int>(state.to_ulong());
                return rtn;
            }
            virtual void SetSPI(ISpi* value) { spi = value; }
	};

    class CDipSwitchManager:public IDipSwitchManager
    {
        static IDipSwitchManager* instance;
        public:
            static IDipSwitchManager* GetInstance();
            //virtual IDipSwitch* NewM01DipSwitch();
			virtual IDipSwitch* New_ProjMCU(const std::string&);	
    };
};

#endif
